let teaId = '',
  teaNo = '';

function loadPage() {
  isLogin();
  layui.use(['element', 'form', 'layer'], () => {
    let form = layui.form,
      layer = layui.layer;
    form.on('submit(edit)', obj => {
      let field = obj.field;
      $.get(`/teacher/queryTeaByNoAndPwd`, {teaNo: teaNo, teaPwd: field.oldPwd}, res => {
        if (res === '') {
          layer.msg('原密码错误');
        } else {
          if (field.newPwd.length === 0) {
            layer.msg('请输入新密码');
            return;
          }
          if (field.reNewPwd !== field.newPwd) {
            layer.msg('两次密码输入不一致');
            return;
          }
          $.post(`${app.urlLocation.updateTeacher}`, {
            id: teaId,
            password: field.newPwd
          }, result => {
            if (result === 200) {
              layer.msg('密码已修改，请重新登录');
              setTimeout(() => {
                window.location.href = '/teacher-action/logout'
              }, 1000);
            } else {
              layer.msg('密码修改失败，请重试或联系系统管理员')
            }
          });
        }
      })
    })
  })
}

function isLogin() {
  $.get(`/teacher/isLogin`, res => {
    if (res === '') {
      window.location.href = '/teacher';
    } else {
      $('.layui-nav img').attr('alt', res.id);
      $('#name').text(res.name);
      teaId = res.id;
      teaNo = res.teacherNo;
    }
  })
}